package com.zhaojiu.example.service.beetl.function;

import org.beetl.core.Context;
import org.beetl.core.Function;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @author huangshb
 * @date 2024/8/5
 */
public class PathToClassName implements Function {
    @Override
    public Object call(Object[] paras, Context ctx) {
        Object o1 = paras[0];
        String path = "";
        if (o1 != null){
            path = o1.toString();
            // todo 用正则优化
            path = path.replace("\\","");
            path = path.replace("{", "").replace("}", "");

            if (path.startsWith("/")) {
                path = path.substring(1);
            }
            String[] strings = path.split("/");
            path =  Arrays.stream(strings)
                    .filter(s-> !StringUtils.isEmpty(s))
                    .map(s -> s.substring(0, 1).toUpperCase() + (s.length() > 1 ? s.substring(1): ""))
                    .collect(Collectors.joining());
        }

        Object o2 = paras[1];
        String requestMethod = "Get";
        if (o2 != null){
            requestMethod = o2.toString();
            switch (requestMethod){
                case "1000":
                    requestMethod = "Post";
                    break;
                case "2000":
                    requestMethod = "Get";
                    break;
                case "3000":
                    requestMethod = "Delete";
                    break;
                case "4000":
                    requestMethod = "Put";
                    break;
                case "5000":
                    requestMethod = "Patch";
                    break;
                default:
                    requestMethod = "Get";
            }
        }
        return requestMethod + path;
    }
}
